package com.jk.garden1.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.garden1.dao.*;
import com.jk.garden1.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static org.springframework.data.elasticsearch.annotations.DateFormat.date;

@Service
@SuppressWarnings("all")
public class PlaneCompanyService {


    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VisitorDao visitorDao;
    @Autowired
    private VisitantDao visitantDao;
    @Autowired
    private FollowPeopleDao followPeopleDao;
    @Autowired
    private BuildingDao buildingDao;
    @Autowired
    private SubscribeDao subscribeDao;

    //定义验证码
    public static String charactercode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


    /**
     * 查询公司名称
     * @param company
     * @return
     */

    public List<CompanyEntity> geiPlaneList(CompanyEntity company) {
        QueryWrapper<CompanyEntity> qw = new QueryWrapper<>();
        if(company.getUbietylevel() !=null && company.getUbietylevel() != "" && company.getBuildingid() !=null){
            qw.eq("ubietylevel",company.getUbietylevel());
            qw.eq("buildingid",company.getBuildingid());
        }
        List<CompanyEntity> companyEntities = companyDao.selectList(qw);
        return companyEntities;

    }

    /**
     * 搜索查询
     * @param company
     * @return
     */
    public List<CompanyEntity> geiPlane(CompanyEntity company) {
        QueryWrapper<CompanyEntity> qw = new QueryWrapper<>();
        if(company.getCompanyname() !=null && company.getCompanyname() != ""){
            qw.like("companyname",company.getCompanyname());
        }
        List<CompanyEntity> companyEntities = companyDao.selectList(qw);
        return companyEntities;
    }

    /**
     *新增访客存redis
     * @param company
     */
    public void addPlane(Map<String,Object> param) {
        Object visitor = param.get("visitor");
        String jsonString = JSON.toJSONString(visitor);
        VisitorEntity visitorEntity = JSON.parseObject(jsonString, VisitorEntity.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        visitorEntity.setVisitorstatus("正常");
        visitorEntity.setVisitorsource("预约");
        visitorEntity.setEntrancedate(sdf.format(new Date()));
        visitorEntity.setCompanyname(param.get("companyname").toString());
        visitorDao.insert(visitorEntity);

        redisTemplate.opsForHash().put("res",visitorEntity.getVisitorid(),visitor);
        redisTemplate.opsForValue().set("res",visitor,120, TimeUnit.SECONDS);
        //通行卡号
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            stringBuilder.append(charactercode.charAt(random.nextInt(6)));
        }
        SubscribeEntity subscribeEntity = new SubscribeEntity();
        subscribeEntity.setThroughcard(stringBuilder.toString());
        subscribeEntity.setVisitantname(visitorEntity.getVisitorname());
        subscribeEntity.setVisitantphone(visitorEntity.getVisitorphone());
        subscribeEntity.setIntervieweename(visitorEntity.getIntervieweename());
        subscribeEntity.setIntervieweecompany(param.get("companyname").toString());
        subscribeEntity.setSubscribetime(param.get("userdate").toString());
        subscribeEntity.setSubscribestatus("预约");
        subscribeDao.insert(subscribeEntity);

        Object follow = param.get("follow");
        String string = JSON.toJSONString(follow);
        List<FollowPeopleEntity> entities = JSON.parseArray(string, FollowPeopleEntity.class);
        entities.forEach(followPeople->{
            followPeople.setVisitorid(visitorEntity.getVisitorid());
            followPeopleDao.insert(followPeople);
        });
    }

    /**
     *新增临时存redis
     * @param company
     */
    public void addPlane11(Map<String,Object> param) {
        Object visitor = param.get("visitor");
        String jsonString = JSON.toJSONString(visitor);
        VisitorEntity visitorEntity = JSON.parseObject(jsonString, VisitorEntity.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        visitorEntity.setVisitorstatus("正常");
        visitorEntity.setVisitorsource("临时登记");
        visitorEntity.setEntrancedate(sdf.format(new Date()));
        visitorDao.insert(visitorEntity);
        redisTemplate.opsForHash().put("src",visitorEntity.getVisitorid(),visitor);
        redisTemplate.opsForValue().set("src",visitor,120, TimeUnit.SECONDS);

        //通行卡号
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            stringBuilder.append(charactercode.charAt(random.nextInt(6)));
        }
        VisitantEntity visitantEntity = new VisitantEntity();
        visitantEntity.setThroughcard(stringBuilder.toString());
        visitantEntity.setVisitantname(visitorEntity.getVisitorname());
        visitantEntity.setVisitantphone(visitorEntity.getVisitorphone());
        visitantEntity.setIntervieweename(visitorEntity.getIntervieweename());
        visitantEntity.setSignintime(new Date().toLocaleString());
        visitantEntity.setSignouttime("2023-3-22 23:59:59");
        visitantEntity.setClassify("临时登记");
        visitantDao.insert(visitantEntity);

        Object follow = param.get("follow");
        String string = JSON.toJSONString(follow);
        List<FollowPeopleEntity> entities = JSON.parseArray(string, FollowPeopleEntity.class);
        entities.forEach(followPeople->{
            followPeople.setVisitorid(visitorEntity.getVisitorid());
            followPeopleDao.insert(followPeople);
        });
    }

        /**
         * 打印访客单（回显访客信息）
         * @param visitorid
         * @return
         */
    public Object getListById(Integer visitorid) {

        return  visitorDao.selectById(visitorid);
    }

//    /**
//     * 有预约（新增访客信息并存入redis）
//     * @param param
//     */
//    public void addAppointmentPlane(Map<String, Object> param) {
//
//
//        Object visitor = param.get("visitor");
//        String jsonString = JSON.toJSONString(visitor);
//        VisitorEntity visitorEntity = JSON.parseObject(jsonString, VisitorEntity.class);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        visitorEntity.setVisitorstatus("正常");
//        visitorEntity.setVisitorsource("临时登记");
//        visitorEntity.setEntrancedate(sdf.format(new Date()));
//        visitorDao.insert(visitorEntity);
//        redisTemplate.opsForHash().put("ress",visitorEntity.getVisitorid(),visitor);
//        redisTemplate.opsForValue().set("ress",visitor,86400, TimeUnit.SECONDS);
//        Object follow = param.get("follow");
//        String string = JSON.toJSONString(follow);
//        List<FollowPeopleEntity> entities = JSON.parseArray(string, FollowPeopleEntity.class);
//        entities.forEach(followPeople->{
//            followPeople.setVisitorid(visitorEntity.getVisitorid());
//            followPeopleDao.insert(followPeople);
//        });
//    }

    /**
     * 查询楼宇
     * @param building
     * @return
     */
    public List<BuildingEntity> getBuildingList(BuildingEntity building) {
        QueryWrapper qw = new QueryWrapper();
        return buildingDao.selectList(qw);
    }

    public List<VisitorEntity> selectList() {
        List<VisitorEntity> list = redisTemplate.opsForHash().values("res");
        return list;
    }


}